#include "bsp_tm1650.h"
#include "string.h"
#include "bsp_param.h"
#include "bsp_beep.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

QueueHandle_t tm1650_led_queue =NULL;

static i2c_config_t conf_master = {
    .mode = I2C_MODE_MASTER,
    .sda_pullup_en = GPIO_PULLUP_DISABLE,
    .scl_pullup_en = GPIO_PULLUP_DISABLE,
    .master.clk_speed = 100000,
    .sda_io_num = I2C_MASTER_SDA_PORT,
    .scl_io_num = I2C_MASTER_SCL_PORT,
    .clk_flags = 0,
};

static void i2c_master_init()
{
    i2c_param_config(I2C_MASTER_NUM, &conf_master);
    i2c_driver_install(I2C_MASTER_NUM, conf_master.mode , 0, 0, 0);
    
}

static void TM1650_Wr_RAM(uint8_t Address, uint8_t Data)
{
    i2c_cmd_handle_t handle = i2c_cmd_link_create();
    i2c_master_start(handle);
    i2c_master_write_byte(handle, Address, true);
    i2c_master_write(handle, &Data, 1, true);
    i2c_master_stop(handle);
    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 0);
    i2c_cmd_link_delete(handle);
}

void TM1650_Init(void)
{
    i2c_master_init();
    tm1650_led_queue = xQueueCreate(5, sizeof(uint32_t));
    TM1650_Wr_RAM(0x48,0x00);       //关闭显示
    TM1650_Wr_RAM(LED_DIG1_ADD,0);
    TM1650_Wr_RAM(LED_DIG2_ADD,0);
    TM1650_Wr_RAM(LED_DIG3_ADD,0);
    TM1650_Wr_RAM(LED_DIG4_ADD,0);
    /* 开显示 */
    TM1650_Wr_RAM(0x48,0x09);
    /* 显示初始值 */

}

/**
 * @brief   按键板 按键状态读取
 * @return  NULL
 */
static uint8_t KEYtm_Scan(void)
{
    uint8_t Temp = KEY_NO_BUTTON_PRESS;
    static uint8_t Temp_old = KEY_NO_BUTTON_PRESS;
    i2c_cmd_handle_t handle = i2c_cmd_link_create();
    i2c_master_start(handle);
    i2c_master_write_byte(handle, 0x4f, true);
    i2c_master_read(handle, &Temp, 1, I2C_MASTER_ACK);
    i2c_master_stop(handle);
    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 0);
    i2c_cmd_link_delete(handle);
    if(Temp == KEY_NO_BUTTON_PRESS){
        return KEY_NO_BUTTON_PRESS;
    }
 
    if(Temp != Temp_old){
        Temp_old = Temp;
        return Temp;
    }
    return KEY_NO_BUTTON_PRESS;
}

/**
 * @brief   按键板led驱动
 * @param   *led_drive  选择需要驱动的led
 */
static void tm1650_led_display(void *led_drive)
{
    tm1650_led_t *tm1650_led = led_drive;

    TM1650_Wr_RAM(LED_DIG1_ADD, tm1650_led->led_dig1);

    TM1650_Wr_RAM(LED_DIG2_ADD, tm1650_led->led_dig2);

    TM1650_Wr_RAM(LED_DIG3_ADD, tm1650_led->led_dig3);

    TM1650_Wr_RAM(LED_DIG4_ADD, tm1650_led->led_dig4);

}



uint32_t tm1650_led_datatrans(void *display_struct)
{
    sys_main_param_t *display_param = NULL;
    display_param = display_struct;

    uint32_t tm1650_trans = 0;
    union dig1_t tm1650_dig1 = {0};
    union dig2_t tm1650_dig2 = {0};
    union dig3_t tm1650_dig3 = {0};
    union dig4_t tm1650_dig4 = {0};
    /* 系统状态 */
    switch(display_param->sys_status){

        case SYS_RUN_NORMAL:
            tm1650_dig4.dig4_bit_t.on = 1;
            tm1650_dig4.dig4_bit_t.stop = 0;
            break;
        case SYS_RUN_FREQCHANGE:
            tm1650_dig4.dig4_bit_t.on = 1;
            tm1650_dig4.dig4_bit_t.stop = 0;
            break;
        case SYS_RUN_HIGHTEMP:
            tm1650_dig4.dig4_bit_t.on = 1;
            tm1650_dig4.dig4_bit_t.stop = 0;
            break;
        case SYS_PAUSE:
            tm1650_dig4.dig4_bit_t.on = 0;
            tm1650_dig4.dig4_bit_t.stop = 1;
            break;
        case SYS_STOP:
            tm1650_dig4.dig4_bit_t.on = 0;
            tm1650_dig4.dig4_bit_t.stop = 1;
            break;
        default:
            break;
    }
    /* xushi模式 */
    switch (display_param->sys_mode)
    {
    case MODE_SHI:
        tm1650_dig1.dig1_bit_t.shi = 1;
        tm1650_dig1.dig1_bit_t.xu = 0;
        break;
    case MODE_XU:
        tm1650_dig1.dig1_bit_t.shi = 0;
        tm1650_dig1.dig1_bit_t.xu = 1;
        break;   
    default:
        break;
    }
    /* 工作时间 */
    if(display_param->sys_runtime == 30*60){
        tm1650_dig2.dig2_bit_t.time_30 = 1;
        tm1650_dig2.dig2_bit_t.time_60 = 0;
        tm1650_dig2.dig2_bit_t.time_90 = 0;
    }else if(display_param->sys_runtime == 60*60){
        tm1650_dig2.dig2_bit_t.time_30 = 0;
        tm1650_dig2.dig2_bit_t.time_60 = 1;
        tm1650_dig2.dig2_bit_t.time_90 = 0;
    }else if(display_param->sys_runtime == 90*60){
        tm1650_dig2.dig2_bit_t.time_30 = 0;
        tm1650_dig2.dig2_bit_t.time_60 = 0;
        tm1650_dig2.dig2_bit_t.time_90 = 1;
    }

    /* 工作强度 */
    switch (display_param->sys_strength)
    {
    case STRENGTH_L:
        tm1650_dig3.dig3_bit_t.temp_60 = 1;
        tm1650_dig3.dig3_bit_t.temp_70 = 0;
        tm1650_dig3.dig3_bit_t.temp_80 = 0;
        break;
    case STRENGTH_M:
        tm1650_dig3.dig3_bit_t.temp_60 = 0;
        tm1650_dig3.dig3_bit_t.temp_70 = 1;
        tm1650_dig3.dig3_bit_t.temp_80 = 0;
        break; 
    case STRENGTH_H:
        tm1650_dig3.dig3_bit_t.temp_60 = 0;
        tm1650_dig3.dig3_bit_t.temp_70 = 0;
        tm1650_dig3.dig3_bit_t.temp_80 = 1;
        break;  
    default:
        break;
    }
    tm1650_trans = ((tm1650_dig4.dig4_byte<<24) | (tm1650_dig3.dig3_byte<<16) | (tm1650_dig2.dig2_byte<<8) | (tm1650_dig1.dig1_byte));
    return tm1650_trans;

}



void task_tm1650(void* arg)
{
    
    uint32_t led_display = 0;
    uint8_t tm1650_key = 0;
    BaseType_t xReturn = pdPASS;
    sys_main_param_t* tm1650_sys_param = Get_sys_param_pos();
    for (;;) {  
        vTaskDelay(50 / portTICK_PERIOD_MS);
        /* led灯点亮 */
        if (xQueueReceive(tm1650_led_queue, &led_display, 0)){
            tm1650_led_display(&led_display);
        }
        /* 按键扫描 */
        tm1650_key = KEYtm_Scan();
        switch(tm1650_key){
            case KEY_DIG4_STRENGTH_PRESS:
                beep_output(BEEP_BUTTON);
                if(tm1650_sys_param->sys_status >= SYS_RUN_NORMAL){
                    break;
                }
                if((++tm1650_sys_param->sys_strength) >= STRENGTH_MAX){
                    tm1650_sys_param->sys_strength = STRENGTH_L;
                }
    
                break;
            case KEY_DIG4_STATUS_PRESS:
                beep_output(BEEP_BUTTON);
                if(tm1650_sys_param->sys_status < SYS_RUN_NORMAL){
                    tm1650_sys_param->sys_status = SYS_RUN_NORMAL;
                }else if(tm1650_sys_param->sys_status >= SYS_RUN_NORMAL){
                    tm1650_sys_param->sys_status = SYS_PAUSE;
                }
                break;
            case KEY_DIG4_TIME_PRESS:
                beep_output(BEEP_BUTTON);
                if(tm1650_sys_param->sys_status >= SYS_RUN_NORMAL){
                    break;
                }else{
                    if((tm1650_sys_param->sys_runtime) >= 90*60){
                        tm1650_sys_param->sys_runtime = 30*60;
                    }else{
                        tm1650_sys_param->sys_runtime += 30*60;
                    }
                }
                break;
            case KEY_DIG4_XUSHI_PRESS:
                beep_output(BEEP_BUTTON);
                if(tm1650_sys_param->sys_status >= SYS_RUN_NORMAL){
                    break;
                }
                if((++tm1650_sys_param->sys_mode) > MODE_XU){
                    tm1650_sys_param->sys_mode = MODE_SHI;
                }
                break; 
            default:
                break;
        }
    }
}



